package cn.edu.qzuie.artist.module.td.dal.mysql.participateactivitiy;

import java.util.*;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.edu.qzuie.artist.framework.mybatis.core.mapper.BaseMapperX;
import cn.edu.qzuie.artist.module.td.controller.admin.work.vo.WorkMultiplePagesVO;
import cn.edu.qzuie.artist.module.td.controller.admin.work.vo.WorkPageReqVO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.activity.ActivityDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.participateactivitiy.ParticipateActivitiyDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.work.WorkDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.workscatagory.WorksCatagoryDO;
import com.alibaba.excel.util.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.edu.qzuie.artist.module.td.controller.admin.participateactivitiy.vo.*;
import org.apache.ibatis.annotations.Select;

/**
 * 参加活动 Mapper
 *
 * @author 泉信管理员
 */
@Mapper
public interface ParticipateActivitiyMapper extends BaseMapperX<ParticipateActivitiyDO> {

    default PageResult<ParticipateActivitiyDO> selectPage(ParticipateActivitiyPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ParticipateActivitiyDO>()
                .eqIfPresent(ParticipateActivitiyDO::getStudentId, reqVO.getStudentId())
                .eqIfPresent(ParticipateActivitiyDO::getActivityId, reqVO.getActivityId())
                .betweenIfPresent(ParticipateActivitiyDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ParticipateActivitiyDO::getId));
    }

    default PageResult<ParticipateActivitiyPagesRespVO> selectPageWithName(PageParam pageParam, ParticipateActivitiyPageReqVO reqVO) {
        // 构建查询条件
        MPJLambdaWrapper<ParticipateActivitiyDO> wrapper = new MPJLambdaWrapper<>();

        // 查询 td_participate_activitiy 表的所有字段
        wrapper.selectAll(ParticipateActivitiyDO.class)
                // 查询
                .select(ActivityDO::getActivityName, ActivityDO::getId)
                .leftJoin(ActivityDO.class, ActivityDO::getId, ParticipateActivitiyDO::getActivityId);
        // 动态拼接查询条件
        if (reqVO.getActivityId() != null) {
            wrapper.eq(ParticipateActivitiyDO::getActivityId, reqVO.getActivityId()); // 精确匹配分类 ID
        }
        if (reqVO.getStudentId() != null) {
            wrapper.eq(ParticipateActivitiyDO::getStudentId, reqVO.getStudentId()); // 精确匹配学生 ID
        }
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(ParticipateActivitiyDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]); // 时间范围查询
        }

        // 默认排序：按 创建时间 倒序
        wrapper.orderByAsc(ParticipateActivitiyDO::getCreateTime);
        // 执行查询并返回结果
        return selectJoinPage(pageParam, ParticipateActivitiyPagesRespVO.class, wrapper);
    }
    //统计已参加人数
    @Select("select count(*) as participants_number from  td_participate_activitiy where activity_id = #{activityId} group by activity_id;")
    Integer selectParticipateNumber(Long activityId);
}